﻿using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using JESAI.Application.Abstractions.Admin;
using JESAI.Application.Abstractions.Admin.Dtos.Api;
using JESAI.Application.Abstractions.Dtos;
using JESAI.AutoMapper.ObjectMapping;
using JESAI.Framework.Filters;
using System;
using System.Linq;

namespace JESAI.Platform.WebApi.Controllers.Admin
{
    /// <summary>
    /// WebApi接口资源管理
    /// </summary>
    [ApiController]
    [Authorize("permission")]
    [IgnoreResultHandlerAttribute]
    public class ApiController : ControllerBase
    {
        private readonly IApiService _apiService;
        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="sqlSugarDbContextFactory"></param>
        /// <param name="mapper"></param>
        public ApiController(IApiService apiService)
        {
            _apiService = apiService;
        }
        /// <summary>
        /// 查询Api资源
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("/Api/QueryApiList")]
        public BasePageOutput<List<ApiDto>> QueryApiList([FromQuery] QueryApiListInput input)
        {
            var list = _apiService.QueryApiList(input);
            return list;
        }

        /// <summary>
        /// 查询Api资源
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("/Api/QueryApis")]
        public BaseOutput<List<ApiDto>> QueryApis([FromQuery] QueryApisInput input)
        {
            var list = _apiService.QueryApis(input);
            return new BaseOutput<List<ApiDto>> { Data = list };
        }
        /// <summary>
        /// 设置Api资源
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("/Api/SetApi")]
        public BaseOutput SetApi([FromBody] SetApiInput input)
        {
            _apiService.UpdateOrInsert(input.MapTo<ApiDto>());

            return new BaseOutput { };
        }

        [HttpGet("/Api/DeleteApi")]
        public BaseOutput DeleteApi(long id)
        {
            _apiService.DeleteApi(id);
            return new BaseOutput { };
        }

        [HttpGet("/Api/DeleteApis")]
        public BaseOutput DeleteApis(long[] ids)
        {
            _apiService.DeleteApis(ids);
            return new BaseOutput { };
        }
    }
}